<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package.Mapper}.${table.mapperName}">
    <resultMap type="${package.Entity}.${entity}" id="${entity}Map">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
                <id property="${field.propertyName}" column="${field.name}"/>
            </#if>
        </#list>
        <#list table.commonFields as field><#--生成公共字段 -->
            <result property="${field.propertyName}" column="${field.name}"/>
        </#list>
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
                <result property="${field.propertyName}" column="${field.name}"/>
            </#if>
        </#list>
    </resultMap>
    <sql id="select${entity}">
        SELECT <#list table.commonFields as field>${field.name},
        </#list>${cfg.fieldNames} FROM ${table.name}
    </sql>
    <select id="selectList" resultMap="${entity}Map">
        <include refid="select${entity}"/>
        <where><#list table.fields as field><#if field.name!='is_active'>
            <#if field.columnType!='INTEGER'><if test ="${field.propertyName} != null and ${field.propertyName} != ''"> and `${field.name}` = ${cfg.mapperSign}${field.propertyName}}</if> </#if><#if field.columnType=='INTEGER'><if test ="${field.propertyName} != null"> and `${field.name}` = ${cfg.mapperSign}${field.propertyName}}</if> </#if></#if></#list>
            <#if cfg.fieldName!='' ><if test ="searchValue != null and searchValue != ''">and ${cfg.fieldName} like CONCAT('%',${cfg.mapperSign}searchValue},'%') </if></#if>
            AND is_active = 1
        </where>
        ORDER BY update_time DESC
    </select>

    <select id="getById" resultMap="${entity}Map">
        <include refid="select${entity}"/> WHERE <#list table.fields as field><#if field.keyFlag>${field.name} = ${cfg.mapperSign}${field.propertyName}}</#if></#list>
        AND is_active = 1
    </select>

    <#list table.fields as field><#if field.keyFlag>
    <insert id="insert" parameterType="${package.Entity}.${entity}" useGeneratedKeys="true" keyProperty="${field.propertyName}"></#if></#list>
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=","><#list table.fields as field> <#if !field.keyFlag>
                <#if field.propertyName=='isActive'>is_active,</#if><#if field.propertyName!='isActive'><if test="${field.propertyName} != null">`${field.name}`,</if></#if></#if></#list>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=","><#list table.fields as field> <#if !field.keyFlag>
             <#if field.propertyName=='isActive'>1,</#if><#if field.propertyName!='isActive'><if test="${field.propertyName} != null">${cfg.mapperSign}${field.propertyName}},</if></#if></#if></#list>
        </trim>
    </insert>

    <#list table.fields as field><#if field.keyFlag>
    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="${field.propertyName}"></#if></#list>
        INSERT INTO ${table.name}(${cfg.fieldNamesNoKey})
        VALUES
        <foreach collection="list" item="param" separator=",">
            (${cfg.batchinsert})
        </foreach>
    </insert>

    <update id="update">
        UPDATE ${table.name}
        <trim prefix="SET" suffixOverrides=","><#list table.fields as field> <#if !field.keyFlag>
            <if test="${field.propertyName} != null">`${field.name}` = ${cfg.mapperSign}${field.propertyName}},</if></#if></#list>
        </trim>
        WHERE <#list table.fields as field><#if field.keyFlag>${field.name} = ${cfg.mapperSign}${field.propertyName}}</#if></#list>
    </update>

    <update id="deleteById">
        UPDATE ${table.name} SET is_active = 0 WHERE <#list table.fields as field><#if field.keyFlag>${field.name} = ${cfg.mapperSign}${field.propertyName}}</#if></#list>
    </update>

    <update id="deleteByIds"><#list table.fields as field><#if field.keyFlag>
        UPDATE ${table.name} SET is_active = 0 WHERE ${field.name} IN
        <foreach collection="array" item="${field.propertyName}" separator="," open="(" close=")">
            ${cfg.mapperSign}${field.propertyName}}
        </foreach></#if></#list>
    </update>

    <select id="checkExists" resultType="${package.Entity}.${entity}">
        <include refid="select${entity}"/>
        <where><#list table.fields as field><#if field.keyFlag>
                <#if field.columnType!='INTEGER'><if test ="${field.propertyName} != null and ${field.propertyName} != ''"> and `${field.name}` = ${cfg.mapperSign}${field.propertyName}}</if> </#if><#if field.columnType=='INTEGER'><if test ="${field.propertyName} != null"> and `${field.name}` = ${cfg.mapperSign}${field.propertyName}}</if> </#if></#if><#if cfg.fieldName==field.name>
            <if test ="${field.propertyName} != null and ${field.propertyName} != ''"> and `${field.name}` = ${cfg.mapperSign}${field.propertyName}}</if></#if></#list>
        </where>
        AND is_active = 1
    </select>
</mapper>